#include "Dlex_parse.h"
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]) {
	switch(argc) {
	case 1 :
		cerr << "Need dlex file.\n";
		cerr << "Usage: dlex filename\n";
		return 1;
	case 2 : {
		ifstream in(argv[1]);		// open the parse file
		if( !in ) {
			cerr << "Open parse file failed.\n";
			return 2;
		}
		string out_name;
		char *p=argv[1];
		while( *p ) {
			if( (*p) == '.' )
				break;
			else {
				out_name += *p;
				p++;
			}
		}
		out_name += ".cpp";
		ofstream out(out_name.c_str());
		if( !out ) {
			cerr << "create out file: \'" << out_name.c_str() << "\' failed.\n";
			return 3;
		}
		try {
			dlex_parse::Parse(in, out, cerr);
		} catch(...) {
			out.close();
			remove(out_name.c_str());
			cerr << "dlex failed.\n";
			return 4;
		}
	break;}
	default :
		cerr << "Too much parametere in your command.\n";
		return 5;
	}
}
